import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Demo_905 {
    public static void main(String[] args)throws InterruptedException {
        //定义一个线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3,5,
                1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                new ThreadPoolExecutor.CallerRunsPolicy());

        //通过循环向线程池中提供任务
        for (int i=0;i<100;i++){
            int takeId = i + 1;
            threadPool.submit(()->{
                System.out.println("执行任务: "+takeId+","+Thread.currentThread().getName());
            });

            if (takeId %2 ==0){
                TimeUnit.SECONDS.sleep(1);
            }
        }
    }
}
